package com.xn.app.muying.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xn.app.muying.MuYingApplication;
import com.xn.app.muying.R;
import com.xn.app.muying.util.Constants;
import com.xn.app.muying.util.ToastUtil;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2016/10/19.
 */
public class YunQiXuanZeActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn1;
    private Button btn2;
    private String result;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yunqixuanze_activity);
        ImageView back = (ImageView) findViewById(R.id.yunqixuanze_activity_back);
        btn1 = (Button) findViewById(R.id.yunqixuanze_activity_btn1);
        btn2 = (Button) findViewById(R.id.yunqixuanze_activity_btn2);
        back.setOnClickListener(this);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.yunqixuanze_activity_back:
                finish();
                break;

            case R.id.yunqixuanze_activity_btn1:
                setbeiyunqi();
                intent.setAction("state");
                intent.putExtra("info", btn1.getText());
                sendBroadcast(intent);
                finish();
                break;

            case R.id.yunqixuanze_activity_btn2:
                //从主页页面(Home)跳转到孕期选择页面执行if,从我的页面(Mine)跳转到孕期选择页面执行else if
                if (getIntent().getStringExtra("home_page").equals("1")) {
                    Intent intent1 = new Intent(YunQiXuanZeActivity.this, JiuZhenActivity.class);
                    intent1.putExtra("tag", 1 + "");
                    intent1.putExtra("value", btn2.getText());
                    intent1.putExtra("home_page", "1");
                    startActivity(intent1);

                    Intent intent2 = new Intent();
                    intent2.setAction("mode");
                    sendBroadcast(intent2);
                    finish();
                } else if (getIntent().getStringExtra("home_page").equals("2")) {
                    intent.putExtra("value", btn2.getText());
                    //setResult(1, intent);
                    intent.putExtra("tag", 1 + "");
//                    intent.setClass(YunQiXuanZeActivity.this, JiuZhenActivity.class);
//                    startActivityForResult(intent, 2);
                    intent.setClass(YunQiXuanZeActivity.this, JiuZhenActivity.class);
                    startActivity(intent);
                    Intent intent1 = new Intent();
                    intent1.setAction("mode");
                    sendBroadcast(intent1);
                    finish();
                }
                break;
        }
    }

    private void setbeiyunqi() {

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    HashMap<String, String> param = new HashMap<>();
                    param.put("pregnancyStatus", 0 + "");

                    String p = new Gson().toJson(param);
                    Log.i("allen==", p);

                    String tokens = Constants.getStringByName(YunQiXuanZeActivity.this, "yonghu");
                    Type type1 = new TypeToken<HashMap<String, String>>() {
                    }.getType();
                    HashMap<String, String> token = new Gson().fromJson(tokens, type1);
                    RequestBody formBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), p);
                    Request request = new Request.Builder().url("http://apitest.beierbaby.com/api/account/pregnancy-info").addHeader("Authorization", "Bearer " + token.get("access_token")).put(formBody).build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();

                    int code = response.code();
                    String bodys = response.body().string();

                    Log.i("code=+=", "code==" + code);
                    Log.i("bodys=+=", bodys);
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        msg2.obj = bodys;
                        handler.sendMessage(msg2);

                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.arg1 = code;
                        msg1.obj = bodys;
                        handler.sendMessage(msg1);
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    ToastUtil.showToast(YunQiXuanZeActivity.this, "修改成功");
                    break;
                case 1:
                    ToastUtil.showToast(YunQiXuanZeActivity.this, "修改失败");

                    break;
            }
        }
    };


//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        result = data.getExtras().getString("yiyuan");
//        id = data.getExtras().getInt("id");
//        Intent intent = new Intent();
//        intent.putExtra("yiyuanming", result);
//        intent.putExtra("yiyuanid", id);
////        intent.putExtra("yunqixinxi",btn2.getText());
//        setResult(1, intent);
//
//    }
}
